//
//  CommonMethod.swift
//  Community
//
//  Created by Vampire on 2019/8/2.
//  Copyright © 2019 ZKJ. All rights reserved.
//

import UIKit

class CommonMethod: NSObject {

    func getRootViewController() -> UIViewController {
        
        let window = UIApplication.shared.keyWindow
        return window?.rootViewController ?? BaseViewController()
    }
    
    
    func getCurrentViewController() -> UIViewController {
        
        var currentViewController = getRootViewController()
        let runLoopFind = true
        while runLoopFind {
            if (currentViewController.presentedViewController != nil) {
                
                currentViewController = currentViewController.presentedViewController ?? BaseViewController()
                
            }else if currentViewController.isKind(of: UINavigationController.self) {
                
                let navigationController = currentViewController as! UINavigationController
                currentViewController = navigationController.children.last ?? BaseViewController()
            }else if currentViewController.isKind(of: UITabBarController.self) {
                
                let tabBarController = currentViewController as! UITabBarController
                currentViewController = tabBarController.selectedViewController ?? BaseViewController()
            }else {
                let childViewControllerCount = currentViewController.children.count
                if childViewControllerCount > 0 {
                    currentViewController = currentViewController.children.last ?? BaseViewController()
                    return currentViewController
                }else {
                    return currentViewController
                }
            }
        }
        return currentViewController
    }
    
    func pushToScanVC(_ callBack:@escaping (_ style:LBXScanViewStyle) -> ()) {
        
        //设置扫码区域参数
        var style = LBXScanViewStyle()
        style.centerUpOffset = 44
        style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle.Inner
        style.photoframeLineW = 3
        style.photoframeAngleW = 18
        style.photoframeAngleH = 18
        style.isNeedShowRetangle = false
        
        style.anmiationStyle = LBXScanViewAnimationStyle.LineMove
        
        //qq里面的线条图片
        style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_scan_light_green")
        callBack(style)
    }
}
